
package galaxy.app.dispatch;

import com.lmax.disruptor.WaitStrategy;

import galaxy.app.Galaxy;
import galaxy.app.dispatch.disruptor.DisruptorService;
import galaxy.app.dispatch.disruptor.ThreadInfo;
import galaxy.app.dispatch.executor.BaseExecutor;
import galaxy.app.log.LogHelper;
import galaxy.kernel.Constants;
import galaxy.kernel.system.AbstractECSSystem;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认的消息派发中心实现,使用 disruptor 实现，也可以自己使用其他方式
 *
 */
public class DispatcherSystem extends AbstractECSSystem {
    DispatcherComponent c;

    public DispatcherSystem() {
        this( 65536);
    }

    public DispatcherSystem(int ringBufferSize) {
        c = Galaxy.components().getSingle(DispatcherComponent.class);
        c.disruptorService = new DisruptorService( ringBufferSize);
        c.disruptorService.addThreads(c.threadinfos);
    }

    public String name() {
        return Constants.Component.DISPATCH;
    }

    @Override
    public void start() {
        c.disruptorService.start();
        LogHelper.info("dispatch init complete!");
    }

    public void stop() {
    }


    public void publish(BaseExecutor executor) {
        c.disruptorService.publish(executor);
    }

    public boolean isEmpty(int threadId) {
        return c.disruptorService.isEmpty(threadId);
    }

    public void addThread(String name, int threadId, int threadNum, WaitStrategy waitStrategy) {
        c.disruptorService.addThread(name, threadId, threadNum, waitStrategy);
    }
}
